package com.qq.BFMRSE.requestModule;
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
public class upLoadFileModule {
	/**
	 * upload all files in the files
	 * @param url
	 * @param files
	 * @param userName
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public static int upLoadFiles(String url, String files,String userName) 
	{
		int stateCode=0;
		File encrytFiles=new File(files);
		if(encrytFiles.exists()&&encrytFiles.isDirectory())
		{
			FileFilter filter=new FileFilter() {
				public boolean accept(File pathname) {
					// TODO Auto-generated method stub
					if(pathname.getName().toLowerCase().endsWith(".txt"))
						return true;
					else
						return false;
				}
			};
			File[]txtEncrFiles=encrytFiles.listFiles(filter);
			HttpClient httpClient=new DefaultHttpClient();
			HttpPost post=new HttpPost(url);
			MultipartEntity multipartEntity=new MultipartEntity();
			try {
				multipartEntity.addPart("userName", new StringBody(userName));
				for(File file:txtEncrFiles)
				{
					FileBody fb=new FileBody(file);
					multipartEntity.addPart(file.getName(),fb);
				}
				post.setEntity(multipartEntity);
				HttpResponse response=httpClient.execute(post);
				stateCode=response.getStatusLine().getStatusCode();
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClientProtocolException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block 
				e.printStackTrace();
			}
		}
		return stateCode;
	}
	
	public static int upLoadSingleFile(String url, String filePath,String userName) 
	{
		int stateCode=0;
		File encrytFile=new File(filePath);
		if(encrytFile.exists()){
			HttpClient httpClient=new DefaultHttpClient();
			HttpPost post=new HttpPost(url);
			MultipartEntity multipartEntity=new MultipartEntity();
			try {
				multipartEntity.addPart("userName", new StringBody(userName));
				FileBody fb=new FileBody(encrytFile);
				multipartEntity.addPart(encrytFile.getName(),fb);
				post.setEntity(multipartEntity);
				HttpResponse response=httpClient.execute(post);
				stateCode=response.getStatusLine().getStatusCode();
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClientProtocolException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block 
				e.printStackTrace();
			}
		}
		return stateCode;
	}
	
	public static boolean deleteDir(File dir){
		if(dir.isDirectory()){
			String []children=dir.list();
			//递归删除目录中的子目录下
            for (int i=0; i<children.length; i++) {
                boolean success = deleteDir(new File(dir, children[i]));
                if (!success) {
                    return false;
                }
            }
        }
        // 目录此时为空，可以删除
        return dir.delete();
        }
}
